<?php

namespace app\console\behavior;

use think\Db;
use think\Url;
use think\Session;
use think\Request;
use think\Controller;

class Config extends Controller
{
    public function run(&$params)
    {
        $CONTROLLER_NAME = Request::instance()->controller();

        if ($CONTROLLER_NAME <> 'Login') {

            // 读取用户配置
            if (empty(cache('varname'))) {


                $configs = Db::name('config')->select();

                foreach ($configs as $config) {
                    cache($config['varname'], $config['varvalue']);
                }

            }

            // 读取权限配置
            if (empty(cache('rule'))) {

                Db::name('auth_rule')->cache('rule',0)->select();

            }

            // 读取广告分类
            if (empty(cache('AdType'))) {

                Db::name('ad_type')->cache('AdType')->select();

            }

            // 读取商品分类
            if (empty(cache('goodsType'))) {

                Db::name('goods_type')->cache('goodsType')->order(['sort' => 'asc'])->select();

            }
            // 会员总数
            $member_sum = Db::name('member')->count();
            $this->assign('member_sum',$member_sum);

            // 成交订单总数
            $member_order_sum = Db::name('order')->where(['status' => ['in', '2,3,4,5,6']])->count();
            $this->assign('member_order_sum',$member_order_sum);

            // 累计充值统计
            Db::name('member_withdraw')->where(['type_id' => '1', 'status' => '2'])
                ->cache('sales_accumulator_recharge', 300)->sum('price');

            // 累计提现统计
            Db::name('member_withdraw')->where(['status' => 1])
                ->cache('sales_accumulator_withdraw', 1)->sum('price');
            // 上架商品
            $stat_goods = Db::name('shop_goods')->where(['state' => '1'])->count();
            $this->assign('stat_goods',$stat_goods);
            // 读取信息分类
            if (empty(cache('informationType'))) {
                Db::name('information_type')->cache('informationType')->select();
            }
            if (empty(cache('member'))) {
                // 上架积分商品
                Db::name('integral_goods')->cache('stat_integral_goods', 300)->count();

                // 累计访问量
                //Db::name('shop')->cache('cumulative_access',300)->sum('hits');

            }


            if (empty(Session::has('manage_id'))) {
                $this->redirect(Url::build('login/index'));
            }
        }
    }
}